//
// Created by hjie on 23-5-14.
//
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <unistd.h>
#include "loop_event.hpp"
#include "./../redis_client.h"
#include "./../redis_message.h"
#include "./../../util/about_time.h"

void ClientThread(LoopEvent & loop)
{
    sleep(2);
    RedisClient * client = new RedisClient(&loop,"127.0.0.1", 6379);

    sleep(1);

    int number = 10;
    std::thread thd([&client, number](){

        hnhj::ComputeTime time;
        time.RecordBegin();
        for (int index = 0; index < 8000; index++)
        {
            int64_t i = client->OperateCommand("set hellloworld kkajajah" + std::to_string(index));
            if (i != -1)
            {
                auto reply = client->GetReplyByIndex(i);
                if (reply && reply->m_reply->IsString())
                {
                    // printf("get index : %ld reply index : %ld\n", i, reply->m_index);

                }
                if (reply)
                {
                    client->FreeReplyByIndex(i);
                }
            
            }
        }
        time.RecordEnd();
        printf("thread %d spend time : %ld\n", number, time.DiffTime());
    });

    number = 11;
    std::thread thd1([&client, number](){

        hnhj::ComputeTime time;
        time.RecordBegin();
        for (int index = 0; index < 4000; index++)
        {
            int64_t i = client->OperateCommand("set hellljjjhhggfoworld kkajajah" + std::to_string(index));
            if (i != -1)
            {
                auto reply = client->GetReplyByIndex(i);
                if (reply && reply->m_reply->IsString())
                {
                    // printf("get index : %ld reply index : %ld\n", i, reply->m_index);

                }
                if (reply)
                {
                    client->FreeReplyByIndex(i);
                }
            
            }
        }
        time.RecordEnd();
        printf("thread %d spend time : %ld\n", number, time.DiffTime());
    });

    hnhj::ComputeTime time;
    time.RecordBegin();
    for (int index = 0; index < 5000; index++)
    {
        int64_t i = client->OperateCommand("set hello" + std::to_string(index) + " worldnumber" + std::to_string(index));
        if (i != -1)
        {
            auto reply = client->GetReplyByIndex(i);
            if (reply && reply->m_reply->IsString())
            {
                // printf("record get index : %ld reply index : %ld\n", i, reply->m_index);
            }
            if (reply)
            {
                client->FreeReplyByIndex(i);
            }
        }
        else 
        {
            std::cout << "operate command error" << std::endl;
        }
    }
    time.RecordEnd();

    std::cout << "time : " << time.DiffTime() << std::endl;

    thd.join();
    thd1.join();

}

void PingClient(LoopEvent & loop)
{
    sleep(2);
    RedisClient * client = new RedisClient(&loop, "127.0.0.1", 6379);

    int64_t  i = client->OperateCommand("ping");
    if (i != -1)
    {
        auto reply = client->GetReplyByIndex(i);
        if (reply && reply->m_reply->IsString())
        {
            printf("record get index : %ld reply content : %s\n", reply->m_index, reply->m_reply->String().c_str());
        }
        if (reply)
        {
            client->FreeReplyByIndex(reply->m_index);
        }
    }
    i = client->OperateCommand("ping hello");
    if (i != -1)
    {
        auto reply = client->GetReplyByIndex(i);
        if (reply && reply->m_reply->IsString())
        {
            printf("record get index : %ld and reply content : %s\n", reply->m_index, reply->m_reply->String().c_str());
        }
        if (reply)
        {
            client->FreeReplyByIndex(reply->m_index);
        }
    }
    i = client->OperateCommand("info");
    if (i > 0)
    {
        auto reply = client->GetReplyByIndex(i);
        if (reply && reply->m_reply->IsString())
        {
            printf("info content : %s\n", reply->m_reply->String().c_str());
        }
        if (reply)
        {
            client->FreeReplyByIndex(i);
        }
    }
    delete client;
}

int main(int argc, char * argv[])
{    
    std::cout << "begin" << std::endl;
    LoopEvent loop;


    std::thread thd(ClientThread, std::ref(loop));

    loop.Run();

    thd.join();

    return 0;
}